#include "Mesh.h"


Mesh::Mesh(void)
{
}
Mesh::~Mesh(void)
{
}

void Mesh::SetVertexBuffer(ID3D11Buffer* VertexBuffer)
{
	mVertexBuffer = VertexBuffer;
}

void Mesh::SetConstantBuffer(ID3D11Buffer* ConstantBuffer)
{
	mConstantBuffer = ConstantBuffer;
}

void Mesh::SetMaterialIndex(int MaterialIndex)
{
	mMaterialIndex = MaterialIndex;
}

void Mesh::SetInputLayout(ID3D11InputLayout* InputLayout)
{
	mInputLayout = InputLayout;
}

void Mesh::SetShaders(ID3D11VertexShader* VertexShader, ID3D11PixelShader* PixelShader)
{
	mVertexShader = VertexShader;
	mPixelShader = PixelShader;
}

ID3D11Buffer* Mesh::GetCB()
{
	return mConstantBuffer;
}

void Mesh::SetWorldMatrix(XMFLOAT4X4 WorldMatrix)
{
	mWorldMatrix = XMLoadFloat4x4(&WorldMatrix);
}

void Mesh::UpdateCB(XMFLOAT4X4 ViewMatrix, XMFLOAT4X4 ProjectionMatrix)
{
	ConstantBufferStruct cb;
	cb.mWorld = DirectX::XMMatrixTranspose( mWorldMatrix );
	XMMATRIX tempView = XMLoadFloat4x4(&ViewMatrix);
	cb.mView = DirectX::XMMatrixTranspose( XMLoadFloat4x4(&ViewMatrix) );

	cb.mProjection = DirectX::XMMatrixTranspose( XMLoadFloat4x4(&ProjectionMatrix) );
	D3DDeviceContext->UpdateSubresource(mConstantBuffer, 0, nullptr, &cb, 0, 0 );
}

void Mesh::Render()
{
	UINT offset = 0;
	D3DDeviceContext->VSSetConstantBuffers( 0, 1, &mConstantBuffer );
	D3DDeviceContext->IASetVertexBuffers( 0, 1, &mVertexBuffer, &mStride, &offset );
	D3DDeviceContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
	D3DDeviceContext->IASetInputLayout(mInputLayout);
	D3DDeviceContext->VSSetShader( mVertexShader, nullptr, 0 );
	D3DDeviceContext->PSSetShader( mPixelShader, nullptr, 0 );
	D3DDeviceContext->Draw(mNumVertices, 0);
}

void Mesh::Initialize(HWND WindowHandle, HINSTANCE hInstance, ID3D11Device* Device, ID3D11DeviceContext* DeviceContext)
{
	D3DDevice = Device;
	D3DDeviceContext = DeviceContext;
	mWorldMatrix = DirectX::XMMatrixIdentity();
}

void Mesh::Update(int DeltaTime)
{

}

void Mesh::Shutdown()
{

}

void Mesh::SetStride(int Stride)
{
	mStride = Stride;
}

void Mesh::SetNumVertices(int NumVertices)
{
	mNumVertices = NumVertices;
}